package com.sun.dailyprj.androidx.viewmodel;


import android.app.Application;

import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.Observer;

import com.sun.dailyprj.androidx.room.RoomAppDatabase;
import com.sun.dailyprj.androidx.room.entity.User;

import java.util.ArrayList;
import java.util.List;

public class VariationViewModel{

    private Application mApp;
    private MediatorLiveData<List<User>> observer;
    public VariationViewModel(Application app){
        this.mApp = app;
        observer = new MediatorLiveData<>();
    }

    public MediatorLiveData<List<User>> test() {
        observer.addSource(RoomAppDatabase.getInstance(mApp).getUserDao().getAllUsers(),
                new Observer<List<User>>(){
                    @Override
                    public void onChanged(List<User> users) {
                        if(users.size() == 0){
                            User user = new User();
                            user.setName("sunyanpeng");
                            user.setSex("male");
                            users.add(user);
                        }
                        observer.postValue(users);
                    }
                });

        new Thread(new Runnable(){
            @Override
            public void run() {
                try{
                    List<User> list = new ArrayList<>();
                    User lisi = new User();
                    lisi.setName("Lisi");
                    lisi.setSex("female");
                    lisi.setRemark("sfhj");
                    list.add(lisi);
                    observer.postValue(list);
                    Thread.sleep(20*1000);
                    User wangwu = new User();
                    wangwu.setName("wangwu");
                    list.add(wangwu);
                    observer.postValue(list);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        return observer;
    }


}
